/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_CONTAINER_CONTAINER_FACTORY_HPP
#define	_CUBEFS_CORE_CONTAINER_CONTAINER_FACTORY_HPP

#define CUBEFS_SOURCE "cubefs/core/container/container/factory.hpp:" CUBEFS_AT

namespace cubefs    {
namespace ctr       {

template <typename Name>
class ContainerPartNotFound;

template <
        typename Name,
        typename MyType,
        typename Base
>
class ContainerPart;


//template <typename Profile, typename Name, typename MyType, typename Base>
//class ContainerPart<NullType, Profile, Name, MyType, Base> {
//public:
//    typedef ContainerPartNotFound<Name>                                         Locate;
//};


template <typename Profile, typename List, typename Result = NullType> class ModelName2TypeMapBuilder;

template <typename Profile, typename Head, typename Tail, typename ResultList>
class ModelName2TypeMapBuilder<Profile, TypeList<Head, Tail>, ResultList> {
    typedef typename TypeLocator<Profile, Head>::Type                           Type;
    typedef TypeList<Pair<Head, Type>, ResultList>                              NewResult;
public:
    typedef typename ModelName2TypeMapBuilder<Profile, Tail, NewResult>::Result Result;
};

template <typename Profile, typename FinalResult>
class ModelName2TypeMapBuilder<Profile, NullType, FinalResult> {
public:
    typedef FinalResult                                                         Result;
};



template <
	typename PageManagerType,
	typename PageDispatcherType,
	typename ModelDispatcherType,
	typename ContainerInterfaceType
>
struct ContainerTypesCollection {
	typedef PageManagerType			PageManager;
	typedef PageDispatcherType		PageDispatcher;
	typedef ModelDispatcherType		ModelDispatcher;
	typedef ContainerInterfaceType	ContainerInterface;
};

template <typename Profile, typename MyType, typename Default>
class TypeFactory<
        RootProfile,
        Profile,
        TypeContainerFactoryName<MyType>, Default
>    {
public:

	typedef typename TypeLocator<
	                        Profile,
	                        ContainerTypesName,
	                        NullType
	                 >::Type                                                	ContainerTypesType;

	typedef typename ContainerTypesType::PageManagerType						PageManagerType;

    typedef typename ModelName2TypeMapBuilder<
							Profile,
							typename ContainerTypesType::RootNamesList
					 >::Result   												Name2TypeMapList;

    typedef PageDispatcher<
							Profile,
							typename ContainerTypesType::Page,
							Name2TypeMapList
			>                           										PageDispatcherType;

    typedef ModelDispatcher<
							Profile,
							PageManagerType,
							Name2TypeMapList
			>         															ModelDispatcherType;


    typedef typename ContainerTypesType::template ContainerBaseTypeFactory<
						Profile,
						MyType,
						ContainerTypesCollection<
							PageManagerType,
							PageDispatcherType,
							ModelDispatcherType,
							typename ContainerTypesType::ContainerInterfaceType
						>
			>::Type																ContainerBaseType;

    typedef TypeAwareBuilder<
            typename ContainerTypesType::ContainerPartsList,
            ContainerPart,
            MyType,
            ContainerBaseType
    >                                                                           Type;

};

}
}

#undef CUBEFS_SOURCE
#endif
